package com.ly.blog2Admin.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ly.blog2Admin.dao.model.SysMenu;
import com.ly.blog2Admin.service.SysMenuService;
import com.ly.blog2Common.core.http.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//import org.springframework.security.access.prepost.PreAuthorize;

/**
 * 菜单控制器
 * @author Louis
 * @date Jan 13, 2019
 */
@RestController
@RequestMapping("menu")
public class SysMenuController {

	@Autowired
	private SysMenuService sysMenuService;
	
	@PreAuthorize("hasAuthority('sys:menu:add') AND hasAuthority('sys:menu:edit')")
	@PostMapping(value="/save")
	public JSONObject save(@RequestBody SysMenu record) {
		return ResponseMessage.ok(sysMenuService.save(record));
	}

	@PreAuthorize("hasAuthority('sys:menu:delete')")
	@PostMapping(value="/delete")
	public JSONObject delete(@RequestBody List<SysMenu> records) {
		return ResponseMessage.ok(sysMenuService.delete(records));
	}

//	@PreAuthorize("hasAuthority('sys:menu:view')")
	@GetMapping(value="/findNavTree")
	public JSONObject findNavTree(@RequestParam(defaultValue = "") String userName) {
		return ResponseMessage.ok(sysMenuService.findTree(userName, 1));
	}

	@GetMapping(value="/findShowMenuTree")
	public JSONObject findShowMenuTree(@RequestParam(defaultValue = "") String userName) {
//		String s = "[\n" +
//				"          {\n" +
//				"            icon:\"el-icon-menu\",\n" +
//				"            name:\"首页\",\n" +
//				"            router:{\n" +
//				"              type:'route',\n" +
//				"              link:'/'\n" +
//				"            }\n" +
//				"          },\n" +
//				"          {\n" +
//				"            icon:\"el-icon-document\",\n" +
//				"            name:\"随笔\",\n" +
//				"            router:{\n" +
//				"              type:'route',\n" +
//				"              link:'/essay'\n" +
//				"            }\n" +
//				"          },\n" +
//				"          {\n" +
//				"            name:\"文章\",\n" +
//				"            router:{\n" +
//				"              type:'route',\n" +
//				"              link:'/article'\n" +
//				"            }\n" +
//				"          },\n" +
//				"          {\n" +
//				"            name:\"CDSN博客\",\n" +
//				"            router:{\n" +
//				"              type:'open',\n" +
//				"              link:'https://blog.csdn.net/YungLee2018'\n" +
//				"            }\n" +
//				"          },\n" +
//				"          {\n" +
//				"            name:\"码云\",\n" +
//				"            router:{\n" +
//				"              type:'open',\n" +
//				"              link:\"https://gitee.com/blog2019/blog\"\n" +
//				"            }\n" +
//				"          },\n" +
//				"        ]";
		JSONArray showMenuList = sysMenuService.findShowMenuList();
		return ResponseMessage.ok(showMenuList);
	}
	
	@PreAuthorize("hasAuthority('sys:menu:view')")
	@GetMapping(value="/findMenuTree")
	public JSONObject findMenuTree() {
		return ResponseMessage.ok(sysMenuService.findTree(null, 0));
	}
}
